package com.pangda.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.pojo.Courses;
import com.pangda.pojo.dto.CoursesDto;
import com.pangda.pojo.vo.CoursesVo;

import com.pangda.service.ICoursesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 课程管理 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-20
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/clues/course")
public class CoursesController {

    @Autowired
    private ICoursesService coursesService;
    @GetMapping("/list")
    public PageResult<CoursesVo> list(@ApiParam("分页参数") Integer pageNum,
                                      @ApiParam("分页参数") Integer pageSize,
                                      CoursesDto coursesDto){

        Object beginCreateTime = coursesDto.getParams().get("beginCreateTime");
        Object endCreateTime = coursesDto.getParams().get("endCreateTime");

        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(coursesDto.getCode()),Courses::getCode,coursesDto.getCode());
        wrapper.eq(ObjectUtil.isNotEmpty(coursesDto.getApplicablePerson()),Courses::getApplicablePerson,coursesDto.getApplicablePerson());
        wrapper.eq(ObjectUtil.isNotEmpty(coursesDto.getSubject()),Courses::getSubject,coursesDto.getSubject());
        wrapper.like(ObjectUtil.isNotEmpty(coursesDto.getName()),Courses::getName,coursesDto.getName());
        wrapper.between(ObjectUtil.isNotEmpty(beginCreateTime) && ObjectUtil.isNotEmpty(endCreateTime),Courses::getCreateTime,beginCreateTime,endCreateTime+" 23:59:59");

        Page<Courses> page = coursesService.page(new Page<>(pageNum, pageSize), wrapper);
        return  PageResult.success(page,CoursesVo.class);
    }

    @GetMapping("/{id}")
    public ResponseResult getInfo(@PathVariable Long id){
        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Courses::getId,id);
        return ResponseResult.success(coursesService.getOne(wrapper));
    }

    @GetMapping("/listselect")
    public ResponseResult listselect(String subject){
        Courses courses = new Courses();
        courses.setSubject(subject);
        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(ObjectUtil.isNotEmpty(courses.getSubject()),Courses::getSubject,subject);

        return ResponseResult.success(coursesService.list(wrapper));
    }


    @PostMapping
    public ResponseResult add(@RequestBody CoursesDto coursesDto){
        String randomAlphabetic = RandomStringUtils.randomAlphabetic(8);
        coursesDto.setCode(randomAlphabetic);

        Courses courses = new Courses();
        courses.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(coursesDto,courses);
        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Courses::getId,courses.getId());

        coursesService.save(courses);
        return ResponseResult.success();
    }

    @PutMapping
    public ResponseResult update(@RequestBody CoursesDto coursesDto){
        Courses courses = new Courses();
        BeanUtils.copyProperties(coursesDto,courses);
        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Courses::getId,courses.getId());
        coursesService.update(courses,wrapper);
        return ResponseResult.success();
    }

    @DeleteMapping("/{ids}")
    public ResponseResult delete(@PathVariable List<String> ids){
        List<Long> list = ids.stream().map(Long::valueOf).collect(Collectors.toList());

        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Courses::getId,list);
        coursesService.remove(wrapper);
        return ResponseResult.success();
    }

}
